package org.jeecg.modules.product.risk.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.common.util.oConvertUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.modules.product.risk.entity.TbBaseCheckDanger;
import org.jeecg.modules.product.risk.service.ITbBaseCheckDangerService;
import org.jeecg.modules.product.risk.vo.TbBaseCheckDangerDto;
import org.jeecgframework.poi.excel.ExcelImportUtil;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.entity.ImportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import com.alibaba.fastjson.JSON;

import org.jeecg.common.aspect.annotation.AutoLog;

 /**
 * @Description: 专项检查隐患信息
 * @Author: jeecg-boot
 * @Date:   2024-05-08
 * @Version: V1.0
 */
@Tag(name="专项检查隐患信息")
@RestController
@RequestMapping("/TbBaseCheckDanger/tbBaseCheckDanger")
@Slf4j
public class TbBaseCheckDangerController extends JeecgController<TbBaseCheckDanger, ITbBaseCheckDangerService> {
	@Autowired
	private ITbBaseCheckDangerService tbBaseCheckDangerService;

	/**
	 * 分页列表查询
	 *
	 * @param tbBaseCheckDanger
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "专项检查隐患信息-分页列表查询")
	@Operation(summary="专项检查隐患信息-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(TbBaseCheckDanger tbBaseCheckDanger,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<TbBaseCheckDanger> queryWrapper = QueryGenerator.initQueryWrapper(tbBaseCheckDanger, req.getParameterMap());
		Page<TbBaseCheckDanger> page = new Page<TbBaseCheckDanger>(pageNo, pageSize);
		queryWrapper.eq("deleted","0");
		IPage<TbBaseCheckDanger> pageList = tbBaseCheckDangerService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param tbBaseCheckDanger
	 * @return
	 */
	@AutoLog(value = "专项检查隐患信息-添加")
	@Operation(summary="专项检查隐患信息-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody TbBaseCheckDanger tbBaseCheckDanger) {
		tbBaseCheckDangerService.save(tbBaseCheckDanger);
		return Result.OK("添加成功！");
	}

	/**
	 *  编辑
	 *
	 * @param tbBaseCheckDanger
	 * @return
	 */
	@AutoLog(value = "专项检查隐患信息-编辑")
	@Operation(summary="专项检查隐患信息-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody TbBaseCheckDanger tbBaseCheckDanger) {
		tbBaseCheckDangerService.updateById(tbBaseCheckDanger);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "专项检查隐患信息-通过id删除")
	@Operation(summary="专项检查隐患信息-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		tbBaseCheckDangerService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "专项检查隐患信息-批量删除")
	@Operation(summary="专项检查隐患信息-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.tbBaseCheckDangerService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	 /**
	  *   通过id逻辑删除
	  *
	  * @param id
	  * @return
	  */
	 @AutoLog(value = "专项检查隐患信息-通过id逻辑删除")
	 @Operation(summary="专项检查隐患信息-通过id逻辑删除")
	 @DeleteMapping(value = "/logicDelete")
	 public Result<?> logicDelete(@RequestParam(name="id",required=true) String id) {
		 TbBaseCheckDanger tbBaseCheckDanger = tbBaseCheckDangerService.getById(id);
		 tbBaseCheckDanger.setDeleted("1");//禁止状态1是已删除，0是正常未删除
		 tbBaseCheckDangerService.updateById(tbBaseCheckDanger);
		 return Result.OK("删除成功!");
	 }

	 /**
	  *  批量逻辑删除
	  *
	  * @param ids
	  * @return
	  */
	 @AutoLog(value = "专项检查隐患信息-批量逻辑删除")
	 @Operation(summary="专项检查隐患信息-批量逻辑删除")
	 @DeleteMapping(value = "/deleteLogicBatch")
	 public Result<?> deleteLogicBatch(@RequestParam(name="ids",required=true) String ids) {
		 List<TbBaseCheckDanger> list = tbBaseCheckDangerService.listByIds(Arrays.asList(ids.split(",")));
		 for (TbBaseCheckDanger tbBaseCheckDanger : list) {
			 tbBaseCheckDanger.setDeleted("1");//禁止状态1是已删除，0是正常未删除
		 }
		 tbBaseCheckDangerService.updateBatchById(list);
		 return Result.OK("批量删除成功！");
	 }

	 /**
	  * 获取被逻辑删除的专项检查隐患信息
	  *
	  * @return logicDeletedUserList
	  */
	 @GetMapping("/recycleBin")
	 public Result getRecycleBin(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		 QueryWrapper<TbBaseCheckDanger> queryWrapper = new QueryWrapper<>();
		 queryWrapper.eq("deleted", "1");//查询已经删除的数据
		 Page<TbBaseCheckDanger> page = new Page<TbBaseCheckDanger>(pageNo, pageSize);
		 IPage<TbBaseCheckDanger> pageList = tbBaseCheckDangerService.page(page, queryWrapper);
		 return Result.ok(pageList);
	 }

	 /**
	  * 还原被逻辑删除的专项检查隐患信息
	  *
	  * @param jsonObject
	  * @return
	  */
	 @RequestMapping(value = "/putRecycleBin", method = RequestMethod.PUT)
	 public Result putRecycleBin(@RequestBody JSONObject jsonObject, HttpServletRequest request) {
		 String tbBaseCheckDangerIds = jsonObject.getString("tbBaseCheckDangerIds");
		 if (StringUtils.isNotBlank(tbBaseCheckDangerIds)) {
			 List<TbBaseCheckDanger> list = tbBaseCheckDangerService.listByIds(Arrays.asList(tbBaseCheckDangerIds.split(",")));
			 for (TbBaseCheckDanger entity : list) {
				 entity.setDeleted("0");
			 }
			 tbBaseCheckDangerService.updateBatchById(list);
		 }
		 return Result.ok("还原成功");
	 }
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "专项检查隐患信息-通过id查询")
	@Operation(summary="专项检查隐患信息-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		TbBaseCheckDanger tbBaseCheckDanger = tbBaseCheckDangerService.getById(id);
		if(tbBaseCheckDanger==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(tbBaseCheckDanger);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param tbBaseCheckDanger
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, TbBaseCheckDanger tbBaseCheckDanger) {
        return super.exportXls(request, tbBaseCheckDanger, TbBaseCheckDanger.class, "专项检查隐患信息");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, TbBaseCheckDanger.class);
    }

	 /**
	  *   添加
	  * @return
	  */
	 @AutoLog(value = "专项检查隐患信息-添加隐患信息和专项检查隐患信息")
	 @Operation(summary="专项检查隐患信息-添加隐患信息和专项检查隐患信息")
	 @PostMapping(value = "/addRecombinationDanger")
	 public Result<?> addRecombinationDanger(@RequestBody TbBaseCheckDangerDto baseCheckDangerDto) {
		 boolean flag = tbBaseCheckDangerService.addRecombinationDanger(baseCheckDangerDto);
		 if(flag){
			 return Result.OK("添加成功！");
		 }else {
			 return Result.error("添加失败！");
		 }
	 }

	 /**
	  *   修改
	  * @return
	  */
	 @AutoLog(value = "专项检查隐患信息-修改隐患信息和专项检查隐患信息")
	 @Operation(summary="专项检查隐患信息-修改隐患信息和专项检查隐患信息")
	 @PostMapping(value = "/editRecombinationDanger")
	 public Result<?> editRecombinationDanger(@RequestBody TbBaseCheckDangerDto baseCheckDangerDto) {
		 boolean flag = tbBaseCheckDangerService.editRecombinationDanger(baseCheckDangerDto);
		 if(flag){
			 return Result.OK("修改成功！");
		 }else {
			 return Result.error("修改失败！");
		 }
	 }

	 /**
	  * 通过id查询复合型隐患数据
	  *
	  * @param id
	  * @return
	  */
	 @AutoLog(value = "专项检查隐患信息-通过id查询复合型隐患数据")
	 @Operation(summary="专项检查隐患信息-通过id查询复合型隐患数据")
	 @GetMapping(value = "/queryRecombinationDangerByDangerId")
	 public Result<?> queryRecombinationDangerByDangerId(@RequestParam(name="id",required=true) String id) {
		 return Result.OK(tbBaseCheckDangerService.queryRecombinationDangerByDangerId(id));
	 }

}
